package logica;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import objetosGraficos.Nave;
import objetosGraficos.NaveJugador;
import frame.FrameJuego;

public class Teclado extends Thread implements KeyListener{

	private int factor = 3;
	private int x1 = 0;
	private int x2 = 0;
	private int y1 = 0;
	private int y2 = 0;

	private NaveJugador nave;
	private FrameJuego juego;

	
	public Teclado(FrameJuego juego, NaveJugador nave){
		this.juego = juego;
		this.nave = nave;		
	}

	public void setNave(NaveJugador nave) {
		this.nave = nave;
	}

	public void setJuego(FrameJuego juego) {
		this.juego = juego;
	}
	
	public void setFactor (int factor){
		this.factor = factor;
	}
	
	public void run (){
		 
		while(nave.getSalud() > 0){
			try{
				Thread.sleep(10); 
				this.nave.setLocation(this.nave.getX() + x1 + x2, this.nave.getY() + y1 + y2);
				rePos();
				this.juego.repaint();
			}catch (InterruptedException ex){}
			
		}
		
	}
	
	
	
	public void keyPressed(KeyEvent arg) {}
	
	public void keyReleased(KeyEvent arg0) {
		switch(arg0.getKeyChar()) {
			case 'w': {
				y1 = 0;
				break;
			}
			case 's': {
				y2 = 0;
				break;
			}
			case 'a': {
				x1 = 0;
				break;
			}
			case 'd': {
				x2 = 0;
				break;
			}
		}
		
	}

	public void keyTyped(KeyEvent arg) {
		switch(arg.getKeyChar()) {
			case 'w': {
				y1 = -factor;
				break;
			}
			case 's': {
				y2 = factor;
				break;
			}
			case 'a': {
				x1 = -factor;
				break;
			}
			case 'd': {
				x2 = factor;
				break;
			}
			case 'j': {
				this.juego.disparo(this.nave.getLocation());
				break;
			}
		}			
	}

		
	private void rePos (){
		if (nave.getY() < 0)
			nave.setLocation(nave.getX(), 0);
		else if (nave.getY() + nave.getHeight() > juego.getHeight() - 28)
			nave.setLocation(nave.getX(),juego.getHeight() - nave.getHeight() - 28);
		
		if (nave.getX() < 0)
			nave.setLocation(0, nave.getY());
		else if (nave.getX() + nave.getWidth() > juego.getWidth() - 6)
			nave.setLocation(juego.getWidth() - nave.getWidth() - 6, nave.getY());
		
	}
	
}

